package org.heart.controller.userInfoController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.heart.common.CommonResult;
import org.heart.entity.dto.UserBaseInfo;
import org.heart.service.IUserBaseInfoService;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户基本信息表 前端控制器
 * </p>
 *
 * @author lml
 * @since 2025-06-16
 */
@RestController
@RequestMapping("/userInfo")
@Slf4j
public class LUserBaseInfoController {

    @Resource
    private IUserBaseInfoService iUserBaseInfoService;

    /**
     * 查询用户详情信息
     * @param userId
     * @return
     */
    @GetMapping("/getUserInfo")
    public CommonResult<UserBaseInfo> getUserInfo(@RequestParam(value = "userId",required = false) String userId) {

        if(StringUtils.isEmpty(userId)){
            return CommonResult.errorResponse("请求失败，必要参数【userId】为空");
        }

        QueryWrapper<UserBaseInfo> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        List<UserBaseInfo> user = iUserBaseInfoService.list(qw);

        if(ObjectUtils.isEmpty(user)){
            return CommonResult.errorResponse("用户不存在！");
        }

        return CommonResult.successResponse(user.get(0));

    }

    /**
     * 编辑用户详情信息
     */
    @PostMapping("/editUserInfo")
    public CommonResult<String> editUserInfo(@RequestBody UserBaseInfo userInfo){

        Boolean st = iUserBaseInfoService.updateById(userInfo);

        if(st){
            return CommonResult.successResponse("更新成功！");
        }else{
            return CommonResult.errorResponse("更新失败！");
        }
    }
}
